package descentFly.away3D4.Generator
{
	import away3d.core.math.Matrix3DUtils;
	import away3d.vf.shapes.vf_star;
	
	import descentFly.app.df_events;
	import descentFly.away3D4.chips.df_chipAway;
	import descentFly.away3D4.df_awaySpace;
	import descentFly.away3D4.df_baseSpaceElement;
	import descentFly.chips.df_chip;
	import descentFly.df_space;
	import descentFly.df_spaceObject;
	import descentFly.df_utils;
	
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	
	public class df_starRain extends df_baseSpaceElement {
		private var _myChip	: df_chipAway;
		private var _stars	: Array;
		public function df_starRain(a_space: df_awaySpace, a_options: Object) {
			super(a_space, a_options);
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			Space.addEventListener(df_space.SETMYCHIP, doSetMyChip);
			setMyChip(Space.myChipAway);
		}

		override protected function removeEvents():void {
			super.removeEvents();
			Space.removeEventListener(df_space.SETMYCHIP, doSetMyChip);
		}
		
		protected function setMyChip(a_myChip: df_chipAway): void {
			if (a_myChip != _myChip) {
				if (_myChip) _myChip.removeEventListener(df_chip.UPDATECHIP, doUpdate);
				_myChip = a_myChip;
				if (_myChip) {
					_myChip.addEventListener(df_chip.UPDATECHIP, doUpdate);
					if (!_stars) initStars();
				}
			}
		}
		
		protected function doSetMyChip(e: Event): void {
			setMyChip(Space.myChipAway);
		}
		
		protected function doUpdate(e: Event): void {
			updateStars();
		}
		
		public function vRnd(scale: Number): Vector3D {
			var box2: Number = options.box / 2;
			var axis	: Vector3D = new Vector3D(Math.random(), Math.random(), Math.random());
			axis.normalize();
			var mat		: Matrix3D = new Matrix3D();
			mat.appendRotation(Math.random() * 360, axis);
			return _myChip.body.currentState.position.add(mat.transformVector(new Vector3D(box2 * scale)));
		}
		
		protected function initStars(): void {
			_stars = new Array();
			for (var i: int=0; i<options.count; i++) {
				var star	: vf_star = new vf_star(options.element.size, options.element);
				star.position = vRnd(Math.random());
				Space.view.view.scene.addChild(star);
				_stars.push(star);
			}
		}
		
		protected function updateStars(): void {
			var box2: Number = options.box / 2;
			var cPos: Vector3D = _myChip.body.currentState.position;
			_stars.forEach(function(star: vf_star, i: int, arr: Array): void {
				var p: Vector3D = cPos.subtract(star.position);
				var len: Number = p.length;
				if (len > box2) {
					p.normalize();
					p.scaleBy(-(2 * box2 - len));
					star.position = vRnd(1); 
				}
			});
		}
		
		protected function removeStars(): void {
			_stars.forEach(function(star: vf_star, i: int, arr: Array): void {
				star.dispose(true);
			});
			_stars.splice(0);
		}
		
		override public function dispose():void {
			removeStars();
			setMyChip(null);
			super.dispose();
		}
	}
}